import GlobalData from "../db/GlobalData";
import StageVO from "../db/vo/StageVO";
import Game from "./Game";
import MonsterData from "./data/MonsterData";
import MonsterVO from "../db/vo/MonsterVO";
import StageData from "./data/StageData";
import GameEvent from "./GameEvent";
import { GameStateType } from "./GameStateType";
import SoundMgr from "../sound/SoundMgr";
import SoundType from "../sound/SoundType";
import HeroVO from "../db/vo/HeroVO";
import GameConfig from "../../GameConfig";
import MonsterScript from "../component/MonsterScript";


export default class GameStage {



    private _stageData: StageData;

    private _stageVO: StageVO;


    private _curCreateMonsterCount = 0;

    public get stageVO() {
        return this._stageVO;
    }

    public init() {
        Laya.stage.on(GameEvent.MISS_MONSTER, this, this.missMonsterHandler);
        Laya.stage.on(GameEvent.KILL_MONSTER, this, this.killMonsterHandler);


    }


    killMonsterHandler(monsterData: MonsterData) {

        this.stageData.killMonsterCount++;
        this.stageData.curMonsterCount++;
        // console.log("killMonsterHandler", this.stageData.curMonsterCount, this._stageVO.monsterCount);
        Laya.stage.event(GameEvent.REFRESH_KILL);
        if (this.stageData.killMonsterCount >= (this._stageVO.killMonsterCount)) {
            Laya.stage.event(GameEvent.GAME_OVER, { isWin: true })
        }
        else if ((this.stageData.curMonsterCount >= this._stageVO.monsterCount)) {
            Laya.stage.event(GameEvent.GAME_OVER, { isWin: true })
        }
    }

    missMonsterHandler(attack) {
        // console.log("receive..GameEvent.MISS_MONSTER,hp=", this.stageData.curHp)
        if (Game.state !== GameStateType.BATTLING) {
            return;
        }
        SoundMgr.Ins.playEffect(SoundType.MISS);
        let hurt = Math.min(attack, this.stageData.curHp);
        this.stageData.curHp -= hurt;
        this.stageData.curMonsterCount++;
        Game.sceneManager.main.gameStageHpView.updateHp(1);
        if (this.stageData.curHp <= 0) {
            Laya.stage.event(GameEvent.GAME_OVER, { isWin: false })
        }
        else {
            if (this.stageData.curMonsterCount >= this._stageVO.monsterCount) {
                if (this.stageData.killMonsterCount <= (this._stageVO.killMonsterCount)) {
                    Laya.stage.event(GameEvent.GAME_OVER, { isWin: false })
                }
            }
        }
    }

    public get stageData() {
        return this._stageData;
    }

    public reset() {
        this._stageData = new StageData();

        let monsterDatas: MonsterData[] = [];
        Laya.timer.clear(this, this.createMonster);
        this._stageVO = GlobalData.getData(GlobalData.StageVO, Game.hero.playerData.mapLevel);
        this._stageData.killMonsterCount = 0;
        this._stageData.curMonsterCount = 0;
        let hpHeroVO: HeroVO = GlobalData.getData(GlobalData.HeroVO, Game.hero.playerData.hpLv);
        this._stageData.maxHp = hpHeroVO.hp;
        this._stageData.curHp = this._stageData.maxHp;
        this._stageData.curLevel = this._stageVO.id;
        console.log(this._stageVO.monsterData)
        let stageMonsterData = JSON.parse(this._stageVO.monsterData);
        // console.log(this._stageData);
        for (let i = 0; i < this._stageVO.monsterIds.length; i++) {
            let monsterVO: MonsterVO = GlobalData.getData(GlobalData.MonsterVO, this._stageVO.monsterIds[i]);
            let monsterData: MonsterData = new MonsterData();

            monsterData.id = this._stageVO.monsterIds[i];
            monsterData.resPath = "res/monster/" + monsterData.id + "/monster" + monsterData.id + ".lh";
            monsterData.moveSpeed = monsterVO.moveSpeedRate * this._stageVO.monsterMoveSpeed;
            monsterData.hp = Math.round(monsterVO.hpRate * this._stageVO.monsterHp);
            monsterData.maxHp = monsterData.hp;
            monsterData.gold = Math.round(monsterVO.dropGoldRate * this._stageVO.dropGold);
            monsterData.attack = monsterVO.attackRate;
            monsterData.diamond = Math.round(monsterVO.dropDiamondRate * this._stageVO.dropDiamond);
            monsterData.skillIds = monsterVO.skillIds;
            monsterData.skillData = stageMonsterData[monsterData.id];
            monsterData.monsterType = monsterVO.monsterType;
            // console.log("monsterData",monsterData)
            // console.log("monsterVO",monsterVO,monsterVO.dropDiamondRate)
            // console.log("heroVO",Game.heroVO)
            // console.log("stageVO",stageVO)
            monsterDatas.push(monsterData);
        }
        // console.log(monsterDatas)
        this._stageData.monsterDatas = monsterDatas;


        Laya.stage.event(GameEvent.LEVEL_UPDATE, this._stageData.curLevel);
    }


    public start() {
        // console.log("_stageData", this._stageData);
        this._curCreateMonsterCount = 0;
        // this._stageVO.monsterCount=1;
        Laya.timer.loop(this._stageVO.createSpeed, this, this.createMonster);
        SoundMgr.Ins.playBg(SoundType.FIGHT_BG);
        // Laya.timer.loop(3800, this, this.createMonster);
        // this.createMonster();
    }

    public createMonster() {
        if (this._curCreateMonsterCount >= this._stageVO.monsterCount) {
            return;
        }
        this._curCreateMonsterCount++;
        let randomIndex = Math.round(Math.random() * (this._stageData.monsterDatas.length - 1));

        let monster = Game.monster.getMonster(this._stageData.monsterDatas[randomIndex]);
        Game.layer3d.addChild(monster);
        monster.getComponent(MonsterScript).run();
        // console.log("randomIndex",this._stageData.monsterDatas)

    }

    public stop() {

    }
}